use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Create sys_user_token table
        manager
            .create_table(
                Table::create()
                    .table(SysUserToken::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysUserToken::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysUserToken::UserId).big_integer().not_null())
                    .col(ColumnDef::new(SysUserToken::AccessToken).string_len(512))
                    .col(ColumnDef::new(SysUserToken::AccessTokenExpire).timestamp())
                    .col(ColumnDef::new(SysUserToken::RefreshToken).string().string_len(512))
                    .col(ColumnDef::new(SysUserToken::RefreshTokenExpire).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysUserToken::TenantId).big_integer())
                    .col(ColumnDef::new(SysUserToken::CreatedAt).timestamp_with_time_zone().not_null())
                    .to_owned(),
            )
            .await?;

        // Create sys_third_login table
        manager
            .create_table(
                Table::create()
                    .table(SysThirdLogin::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysThirdLogin::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysThirdLogin::OpenType).string())
                    .col(ColumnDef::new(SysThirdLogin::OpenId).string())
                    .col(ColumnDef::new(SysThirdLogin::Username).string())
                    .col(ColumnDef::new(SysThirdLogin::UserId).big_integer())
                    .col(ColumnDef::new(SysThirdLogin::TenantId).big_integer())
                    .col(ColumnDef::new(SysThirdLogin::Version).integer().default(0))
                    .col(ColumnDef::new(SysThirdLogin::Deleted).integer().default(0))
                    .col(ColumnDef::new(SysThirdLogin::CreatedAt).timestamp_with_time_zone().not_null())
                    .to_owned(),
            )
            .await?;

        // Create sys_third_login_config table
        manager
            .create_table(
                Table::create()
                    .table(SysThirdLoginConfig::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysThirdLoginConfig::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysThirdLoginConfig::OpenType).string())
                    .col(ColumnDef::new(SysThirdLoginConfig::ClientId).string())
                    .col(ColumnDef::new(SysThirdLoginConfig::ClientSecret).string())
                    .col(ColumnDef::new(SysThirdLoginConfig::RedirectUri).string())
                    .col(ColumnDef::new(SysThirdLoginConfig::AgentId).string())
                    .col(ColumnDef::new(SysThirdLoginConfig::TenantId).big_integer())
                    .col(ColumnDef::new(SysThirdLoginConfig::Version).integer().default(0))
                    .col(ColumnDef::new(SysThirdLoginConfig::Deleted).integer().default(0))
                    .col(ColumnDef::new(SysThirdLoginConfig::CreatedAt).timestamp_with_time_zone().not_null())
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysThirdLoginConfig::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysThirdLogin::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysUserToken::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum SysUserToken {
    Table,
    Id,
    UserId,
    AccessToken,
    AccessTokenExpire,
    RefreshToken,
    RefreshTokenExpire,
    TenantId,
    CreatedAt,
}

#[derive(DeriveIden)]
enum SysThirdLogin {
    Table,
    Id,
    OpenType,
    OpenId,
    Username,
    UserId,
    TenantId,
    Version,
    Deleted,
    CreatedAt,
}

#[derive(DeriveIden)]
enum SysThirdLoginConfig {
    Table,
    Id,
    OpenType,
    ClientId,
    ClientSecret,
    RedirectUri,
    AgentId,
    TenantId,
    Version,
    Deleted,
    CreatedAt,
}